/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cont;

import db.ShoppingDB;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import shopping.Item;

/**
 *
 * @author mpower
 */
public class ControllerServlet extends HttpServlet {
   
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        String command=request.getParameter("command");
        System.out.println("Command====>"+command);
        HttpSession session=request.getSession();
        List<Item> cart=(List<Item>)session.getAttribute("cart");
        RequestDispatcher rd;
        if(cart==null) {
            cart=new ArrayList<Item>();
        }
        if(command.equals("addCart")) {
            String itemString=request.getParameter("item");
            int qty=Integer.parseInt(request.getParameter("qty").trim());
            String[] itemMembers=itemString.split(",");
            Item item=new Item(itemMembers[0],itemMembers[1],qty,Double.parseDouble(itemMembers[2]));
            cart.add(item);
            session.setAttribute("cart", cart);
            response.sendRedirect(request.getContextPath()+"/home.jsp");
        } else if(command.equals("checkout")) {
            try{
                ShoppingDB.addItems(cart);
            }
            catch(SQLException e) {
                e.printStackTrace();
            }
            response.sendRedirect(request.getContextPath()+"/checkout.jsp");
        } else if(command.equals("removeItem")) {
            String itemCode=request.getParameter("itemCode");
            Item removeItem=new Item();
            removeItem.setItemCode(itemCode);
            cart.remove(removeItem);
            session.setAttribute("cart", cart);
            response.sendRedirect(request.getContextPath()+"/home.jsp");
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
